home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Log / syslog.php < prev    next >
PHP Script  |  2004-10-01  |  5KB  |  161 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/syslog.php,v 1.22 2004/01/19 08:02:40 jon Exp $
  4.  * $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
  5.  *
  6.  * @version $Revision: 1.22 $
  7.  * @package Log
  8.  */
  9.  
  10. /**
  11.  * The Log_syslog class is a concrete implementation of the Log::
  12.  * abstract class which sends messages to syslog on UNIX-like machines
  13.  * (PHP emulates this with the Event Log on Windows machines).
  14.  *
  15.  * @author  Chuck Hagenbuch <chuck@horde.org>
  16.  * @since   Horde 1.3
  17.  * @since   Log 1.0
  18.  * @package Log
  19.  *
  20.  * @example syslog.php      Using the syslog handler.
  21.  */
  22. class Log_syslog extends Log
  23. {
  24.     /**
  25.     * Integer holding the log facility to use. 
  26.     * @var string
  27.     * @access private
  28.     */
  29.     var $_name = LOG_SYSLOG;
  30.  
  31.     /**
  32.      * Constructs a new syslog object.
  33.      *
  34.      * @param string $name     The syslog facility.
  35.      * @param string $ident    The identity string.
  36.      * @param array  $conf     The configuration array.
  37.      * @param int    $level    Log messages up to and including this level.
  38.      * @access public
  39.      */
  40.     function Log_syslog($name, $ident = '', $conf = array(),
  41.                         $level = PEAR_LOG_DEBUG)
  42.     {
  43.         /* Ensure we have a valid integer value for $name. */
  44.         if (empty($name) || !is_int($name)) {
  45.             $name = LOG_SYSLOG;
  46.         }
  47.  
  48.         $this->_id = md5(microtime());
  49.         $this->_name = $name;
  50.         $this->_ident = $ident;
  51.         $this->_mask = Log::UPTO($level);
  52.     }
  53.  
  54.     /**
  55.      * Opens a connection to the system logger, if it has not already
  56.      * been opened.  This is implicitly called by log(), if necessary.
  57.      * @access public
  58.      */
  59.     function open()
  60.     {
  61.         if (!$this->_opened) {
  62.             openlog($this->_ident, LOG_PID, $this->_name);
  63.             $this->_opened = true;
  64.         }
  65.  
  66.         return $this->_opened;
  67.     }
  68.  
  69.     /**
  70.      * Closes the connection to the system logger, if it is open.
  71.      * @access public
  72.      */
  73.     function close()
  74.     {
  75.         if ($this->_opened) {
  76.             closelog();
  77.             $this->_opened = false;
  78.         }
  79.  
  80.         return ($this->_opened === false);
  81.     }
  82.  
  83.     /**
  84.      * Sends $message to the currently open syslog connection.  Calls
  85.      * open() if necessary. Also passes the message along to any Log_observer
  86.      * instances that are observing this Log.
  87.      *
  88.      * @param mixed $message String or object containing the message to log.
  89.      * @param int $priority (optional) The priority of the message.  Valid
  90.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  91.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  92.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  93.      * @return boolean  True on success or false on failure.
  94.      * @access public
  95.      */
  96.     function log($message, $priority = null)
  97.     {
  98.         /* If a priority hasn't been specified, use the default value. */
  99.         if ($priority === null) {
  100.             $priority = $this->_priority;
  101.         }
  102.  
  103.         /* Abort early if the priority is above the maximum logging level. */
  104.         if (!$this->_isMasked($priority)) {
  105.             return false;
  106.         }
  107.  
  108.         /* If the connection isn't open and can't be opened, return failure. */
  109.         if (!$this->_opened && !$this->open()) {
  110.             return false;
  111.         }
  112.  
  113.         /* Extract the string representation of the message. */
  114.         $message = $this->_extractMessage($message);
  115.  
  116.         if (!syslog($this->_toSyslog($priority), $message)) {
  117.             return false;
  118.         }
  119.  
  120.         $this->_announce(array('priority' => $priority, 'message' => $message));
  121.  
  122.         return true;
  123.     }
  124.  
  125.     /**
  126.      * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
  127.      *
  128.      * This function exists because, under Windows, not all of the LOG_*
  129.      * constants have unique values.  Instead, the PEAR_LOG_* were introduced
  130.      * for global use, with the conversion to the LOG_* constants kept local to
  131.      * to the syslog driver.
  132.      *
  133.      * @param int $priority     PEAR_LOG_* value to convert to LOG_* value.
  134.      *
  135.      * @return  The LOG_* representation of $priority.
  136.      *
  137.      * @access private
  138.      */
  139.     function _toSyslog($priority)
  140.     {
  141.         static $priorities = array(
  142.             PEAR_LOG_EMERG   => LOG_EMERG,
  143.             PEAR_LOG_ALERT   => LOG_ALERT,
  144.             PEAR_LOG_CRIT    => LOG_CRIT,
  145.             PEAR_LOG_ERR     => LOG_ERR,
  146.             PEAR_LOG_WARNING => LOG_WARNING,
  147.             PEAR_LOG_NOTICE  => LOG_NOTICE,
  148.             PEAR_LOG_INFO    => LOG_INFO,
  149.             PEAR_LOG_DEBUG   => LOG_DEBUG
  150.         );
  151.  
  152.         /* If we're passed an unknown priority, default to LOG_INFO. */
  153.         if (!is_int($priority) || !in_array($priority, $priorities)) {
  154.             return LOG_INFO;
  155.         }
  156.  
  157.         return $priorities[$priority];
  158.     }
  159. }
  160. ?>
  161.